﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECP.Utilities.Controls;
using ECP.Utilities;
using ECP.Model;
using ECP.Service.Utilities;

namespace ECP.Client.Module
{
    public partial class ModuleAddFrm : FormBase
    {
       
        private DataTable DTModule;
        public string EntityID;
        public string SelectID = "";

        public ModuleAddFrm()
        {
            this.DTModule = new DataTable(BaseModuleTable.TableName);
            this.EntityID = string.Empty;
            this.InitializeComponent();
        }

        public ModuleAddFrm(string id)
            : this()
        {
            this.EntityID = id;
        }

        public ModuleAddFrm(string moduleID, string moduleFullName)
            : this(string.Empty)
        {
            this.ucParent.SelectModule.selectedID = moduleID;
            this.ucParent.SelectModule.selectedFullName = moduleFullName;
            this.ucParent.SetControlState();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        base.Close();
                    }
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private bool CheckInput()
        {
            this.txtCode.Text = this.txtCode.Text.TrimEnd(new char[0]);
            if (this.txtCode.Text.Trim().Length == 0)
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { AppMessage.MSG9977 }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtCode.Focus();
                return false;
            }
            this.txtFullName.Text = this.txtFullName.Text.TrimEnd(new char[0]);
            if (this.txtFullName.Text.Trim().Length == 0)
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { AppMessage.MSG9978 }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtFullName.Focus();
                return false;
            }
            return true;
        }

        private bool SaveEntity()
        {
            bool flag = false;
            this.EntityToDataTable();
            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            this.EntityID = ServiceManager.Instance.ModuleService.Add(base.UserInfo, this.DTModule, out statusCode, out statusMessage);
            if (statusCode == StatusCode.OKAdd.ToString())
            {
                if (BaseSystemInfo.ShowInformation)
                {
                   XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                flag = true;
            }
            else
            {
               XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                if (statusCode == StatusCode.ErrorCodeExist.ToString())
                {
                    this.txtCode.SelectAll();
                    this.txtCode.Focus();
                }
            }
            this.Cursor = Cursors.Default;
            return flag;
        }

        private void ShowEntity()
        {
            if (this.DTModule.Rows.Count == 1)
            {
                BaseModuleEntity entity = new BaseModuleEntity(this.DTModule);
                this.txtCode.Text = entity.Code;
                this.txtFullName.Text = entity.FullName;
                this.txtWinForm.Text = entity.WinForm;
                this.txtNavigateUrl.Text = entity.NavigateUrl;
                this.txtTarget.Text = entity.Target;
                this.chkIsGroup.Checked = entity.IsGroup;
                this.chkEnabled.Checked = entity.Enabled;
                this.chkIsPublic.Checked = entity.IsPublic;
                this.txtDescription.Text = entity.Description;
            }
        }

        private void EntityToDataTable()
        {
            if (this.DTModule.Rows.Count == 0)
            {
                this.DTModule.Rows.Add(this.DTModule.NewRow());
            }
            DataRow row = this.DTModule.Rows[0];
            row[BaseModuleTable.FieldParentID] = (this.ucParent.SelectModule.selectedID != null) ? this.ucParent.SelectModule.selectedID.ToString() : string.Empty;
            row[BaseModuleTable.FieldCode] = this.txtCode.Text;
            row[BaseModuleTable.FieldFullName] = this.txtFullName.Text;
            row[BaseModuleTable.FieldWinForm] = this.txtWinForm.Text;
            row[BaseModuleTable.FieldNavigateUrl] = this.txtNavigateUrl.Text;
            row[BaseModuleTable.FieldTarget] = this.txtTarget.Text;
            row[BaseModuleTable.FieldEnabled] = this.chkEnabled.Checked ? 1 : 0;
            row[BaseModuleTable.FieldIsPublic] = this.chkIsPublic.Checked ? 1 : 0;
            row[BaseModuleTable.FieldIsGroup] = this.chkIsGroup.Checked ? 1 : 0;
            row[BaseModuleTable.FieldDescription] = this.txtDescription.Text;
            this.DTModule.AcceptChanges();
        }

        private void FormOnLoad()
        {
            base.Localization(this);
            this.GetPermission();
            this.DTModule = ServiceManager.Instance.ModuleService.Get(base.UserInfo, this.EntityID);
            this.ShowEntity();
            base.ActiveControl = this.txtCode;
            this.txtCode.Focus();
            var selectModule = new UCModuleSelect.SelectModuleInfo();

            selectModule.form=new ModuleSelectFrm();
            selectModule.selectedID = this.ucParent.SelectModule.selectedID;
            selectModule.selectedFullName = this.ucParent.SelectModule.selectedFullName;
            this.ucParent.SelectModule = selectModule;
        }
        private void GetPermission()
        {
            bool isAdministrator = base.UserInfo.IsAdministrator;
        }
        private void ModuleAddFrm_KeyDown(object sender, KeyEventArgs e)
        {
            string str2;
            if ((((str2 = e.KeyCode.ToString()) != null) && ((str2 == "Enter") || (str2 == "Return"))) && ((base.ActiveControl is TextBox) && !((TextBox)base.ActiveControl).Multiline))
            {
                SendKeys.Send("{TAB}");
            }
            int keyValue = e.KeyValue;
            if ((keyValue != 0x2e) && (keyValue == 0x74))
            {
                this.FormOnLoad();
            }
        }

        private void ModuleAddFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.FormOnLoad();
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ucParent_SelectedIndexChanged(string selectedID)
        {
            SelectID = ucParent.SelectModule.selectedID;
        }

    }
}